package dao;

import model.Classes;
import utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author tongchen
 * @create 2022-12-28 13:03
 */
public class ClassDao {
    public Classes getClassIdByName(String name){
        //交互过程中出现的变量
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        //建立连接
        try {
             connection = DBUtils.getConnection();
            //编写sql语句
            String sql="select id,name,descs from classes where name=?";
            //建立执行器
             statement = connection.prepareStatement(sql);
             //补齐参数
            statement.setString(1,name);
            //执行结果
             resultSet = statement.executeQuery();
            //创建模型保存数据
            Classes classes=null;
             if(resultSet.next()){
                 classes=new Classes();
                 classes.setId(resultSet.getInt(1));
                 classes.setName(resultSet.getString(2));
                 classes.setDescs(resultSet.getString(3));
             }
            return classes;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //释放资源
            DBUtils.closeResources(connection,statement,resultSet);
        }

    }
}
